package com.jy.wms.service;

import PTLwebService.*;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsInventoryPlanMapper;
import com.jy.wms.dao.WmsInventoryTaskMapper;
import com.jy.wms.pojo.WmsInventoryPlan;
import com.jy.wms.pojo.WmsInventoryTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

@Service
@WebService(
        targetNamespace = "http://webservice.cxf.com/"// 与接口中的命名空间一致,一般是接口的包名倒
)
@Component
public class CxfInventoryPlanService extends BaseService<WmsInventoryTaskMapper,WmsInventoryTask> {
    /**
     * 推送一个盘点任务明细（匹配有值的字段）
     *
     * @param planId 盘点ID
     * @return int
     */
    @Autowired
    private WmsInventoryPlanMapper wmsInventoryPlanMapper;

    public CheckInventory addCxfInventoryPlan(String planId)throws ServiceException {
        CheckInventory plans = new CheckInventory();//用于返回结果
        WmsInventoryPlan plan1= wmsInventoryPlanMapper.baseSelectByPrimaryKey(planId);//查询盘点信息判断状态是否符合
        if("1".equals(plan1.getRwzt())) {
            //修改分配状态
            WmsInventoryPlan plan = new WmsInventoryPlan();
            plan.setFpzt("1");
            plan.setId(planId);
            wmsInventoryPlanMapper.baseUpdateByPrimaryKeySelective(plan);//更新分配状态
            List<WmsInventoryTask> tasks = dao.seachXxByPdId(planId);//查询盘点任务明细

            plans.setID(UUIDUtils.uuid());
            plans.setPlanId(tasks.get(0).getPlanId());
            plans.setPlanNo(tasks.get(0).getPdbh());
            List<Goods> goodss = new ArrayList<>();
            ArrayOfGoods goodsList = new ArrayOfGoods();
            Goods goods = null;
            for (WmsInventoryTask task : tasks) {
                goods = new Goods();
                goods.setLocationName(task.getKwmc());
                goods.setGoodsnumber(task.getGoodsBh());
                goods.setPlanNum(task.getJhsl().intValue());
                goodss.add(goods);
            }
            goodsList.setGoods(goodss);
            SendCheckInventoryDataResponse response = new SendCheckInventoryDataResponse();
            Result result = new Result();
            response.setSendCheckInventoryDataResult(result);
        }else{
             throw new ServiceException("此盘点计划未生成盘点任务不能下发");
        }
        return plans;
    }
}
